package com.example.demo.service;

import com.example.demo.Mapper.UserMapper;
import com.example.demo.model.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.NESTED)
    public int add(Userinfo userinfo){
        int result=userMapper.add(userinfo);
        System.out.println("add result->"+result);
        insert(userinfo);
        return result;
    }

    @Transactional(propagation = Propagation.NESTED)
    public int insert(Userinfo userinfo){
        int result=userMapper.add(userinfo);
        System.out.println("add result->"+result);
        try {
            int num=10/0;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }
}
